import sys

entrance_way = ""
use_npu = False

entrance_index = 1
for index, arg in enumerate(sys.argv):
    if arg.endswith("detector.py"):
        entrance_index = index + 1
        break
else:
    print("Wrong running method.")
    sys.exit()

try:
    entrance_way = sys.argv[entrance_index]
    if entrance_way == "--npu":
        use_npu = True
        entrance_index += 1
        entrance_way = sys.argv[entrance_index]
except IndexError:
    pass

if entrance_way == "--gui":
    import gui_entrance
    gui_entrance.show_window(use_npu)
elif entrance_way == "--cli":
    import cli_entrance

    video_file = ""
    interval = 0
    port = 8888
    frame_interval = 120

    try:
        for index, arg in enumerate(sys.argv[entrance_index + 1:], start=entrance_index + 1):
            if arg == "--video":
                video_file = sys.argv[index + 1]
            if arg == "--interval":
                interval = int(sys.argv[index + 1])
            if arg == "--port":
                port = int(sys.argv[index + 1])
            if arg == "--frame-interval":
                frame_interval = int(sys.argv[index + 1])

    except:
        pass

    if video_file != "":
        cli_entrance.enter(video_file, interval, port, frame_interval, use_npu)
    else:
        print("No video file inputted, setting one by `-video` argument first please.")
else:
    print("Usage: python3 detector.py [--npu] <--gui|--cli|--help> [CLI options...]")
    print(" --npu\tUse NPU to run the detector whose default mode is CPU with RVV.")
    print()
    print(" --gui\tShow GUI interface (No other arguments needed).")
    print(" --cli\tEnter CLI interface. Must use `--video` to set a video file.")
    print(" --help\tGet help for commands.")
    print()
    print("CLI Options:")
    print(" --video <file>  \tSet a monitoring video file for the detection test.")
    print(" --interval <time>  \tSet the interval between two video frames in seconds (default is 0).")
    print(" --port <port>   \tSet the port of the server (default is 8888).")
    print(" --frame-interval <integer>  \tSet the the interval frame between two detections (default is 120 frames).")
